import numpy as np
from sklearn.metrics import auc, roc_curve, accuracy_score, classification_report, precision_score, recall_score, \
    f1_score
from sklearn.model_selection import StratifiedKFold  # 分层k折交叉验证
import math
import joblib
from mdp_random import data_handle, plot_roc
from sklearn import metrics
import matplotlib.pyplot as plt
from collections import Counter
from test_random import random_result
from test_dnn import dnn_result
from test_SVM import svm_result
from test_DT import DT_result


def display(filename):
    datasets, labels, count = data_handle(filename)
    X_test = datasets[:]
    Y_test = labels[:]
    list_prediction = []
    list_color = ['blue','yellow', 'green', 'orange']

    clf0 = joblib.load('./files/random.pkl')
    y_predict0 = clf0.predict(X_test)
    list_prediction.append(y_predict0)

    clf1 = joblib.load('./files/dnn.pkl')
    y_predict1 = clf1.predict(X_test)
    list_prediction.append(y_predict1)

    clf2 = joblib.load('./files/svm.pkl')
    y_predict2 = clf2.predict(X_test)
    list_prediction.append(y_predict2)

    clf3 = joblib.load('./files/dt.pkl')
    y_predict3 = clf3.predict(X_test)
    list_prediction.append(y_predict3)

    axes = plt.subplots(ncols=1, nrows=1, figsize=(6.5, 6.5), dpi=100)

    plt.ylabel('TPR（真阳性率）')
    plt.xlabel('FPR（伪阳性率）')
    plt.rcParams["font.sans-serif"] = ["SimHei"]
    i = 0
    for y_predict in list_prediction:
        # accuracy = accuracy_score(Y_test, y_predict)
        # precision = precision_score(Y_test, y_predict, average='weighted')
        # recall = recall_score(Y_test, y_predict, average='weighted')
        # f1score = f1_score(Y_test, y_predict, average='weighted')
        # false_positive_rate, true_positive_rate, thresholds = roc_curve(Y_test, y_predict)
        # roc_auc = auc(false_positive_rate, true_positive_rate)
        #
        # false_positive_rate, true_positive_rate = false_positive_rate[1], true_positive_rate[1]
        # g_mean = math.sqrt(true_positive_rate * (1 - false_positive_rate))
        # balance = 1 - math.sqrt(
        #     math.pow((1 - true_positive_rate), 2) + math.pow((0 - false_positive_rate), 2)) / math.sqrt(2)

        false_positive_rate, true_positive_rate, thresholds = metrics.roc_curve(Y_test, y_predict)  # 真阳性，假阳性，阈值
        roc_auc = metrics.auc(false_positive_rate, true_positive_rate)  # 计算AUC值
        plt.plot(false_positive_rate, true_positive_rate, 'b', label='AUC = %0.4f' % roc_auc, color=list_color[i])
        i = i + 1

    plt.legend(['random forest', 'deep neuron network', 'support vector machine', 'decision tree'])
    plt.plot([0, 1], [0, 1], 'r--')
    plt.title('PC5-ROC')
    plt.show()


if __name__ == '__main__':
    display('MDP/ClassLevel6000+.csv')
